<?php

declare(strict_types=1);

namespace App\Model;

use Hyperf\DbConnection\Model\Model;
use Hyperf\Database\Model\SoftDeletes;
use App\Model\User as UserModel;
use Hyperf\Amqp\Producer;
use App\Amqp\Producer\ChatProducer;
use Hyperf\Context\ApplicationContext;

/**
 * @property int $id 
 * @property int $userid 
 * @property int $friendid 
 * @property string $content 
 * @property \Carbon\Carbon $created_at 
 * @property \Carbon\Carbon $updated_at 
 * @property string $deleted_at 
 */
class Chat extends Model
{
    use SoftDeletes;
	
    /**
     * The table associated with the model.
     */
    protected ?string $table = 'chat';

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = [];

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = ['id' => 'integer', 'userid' => 'integer', 'friendid' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime'];

    /**
	 * @var bool
	 */
//    public bool $timestamps = false;

	// 获取对话记录
	public static function getList($token, $friendId,$userId)
	{
	    $listOne = self::where(['userid' => $userId, 'friendid' => $friendId])->orderBy('created_at', 'asc')->get();
	    $listTwo = self::where(['userid' => $friendId, 'friendid' => $userId])->orderBy('created_at', 'asc')->get();
	    $list = $listOne->merge($listTwo)->toArray();
         $data = array_column($list, 'id');
         array_multisort($data, SORT_ASC, $list);
         
	    return $list;
	}

    //对话增加
	public static function chat_add($chat)
	{
	    $message = new ChatProducer($chat);
	    $producer = ApplicationContext::getContainer()->get(Producer::class);
	    return $producer->produce($message);
	}
}
